<?php
class Merchant_IndexController extends Zend_Controller_Action
{

	public function preDispatch(){

	}
	/**
	* 用户注册处理程序
	*
	*/
	public function registerAction(){
		$row = array(
			"customer_email" => trim($this->_request->customerEmail),
			"customer_password" => trim($this->_request->password),
			"rePassword" => trim($this->_request->rePassword),
			"customer_last_update" => date("Y-m-d"),
			"customer_date_reg" => date("Y-m-d H:i:s")
		);

		$customerServiceObj = new Merchant_Service_Customer();
		$registerResult = $customerServiceObj->register($row);
		if($registerResult === true){//注册成功跳转到用户中心
			die(json_encode(array(
				"ask" => true,
			)));
		}else if(is_array($registerResult) && !empty($registerResult)){//失败返回首页面并提示错误信息
			die(json_encode(array(
				"ask" => false,
				"error" => $registerResult,
				"row" => $row
			)));
		}
	}
	
	/**
	*
	*
	*/
	public function loginAction(){
        $lang=isset($_COOKIE['LANGUAGES'])?$_COOKIE['LANGUAGES']:'en';
		//开始登录
		$loginEmail = trim($this->_request->loginEmail);
		$password = trim($this->_request->password);
		$customerServiceObj = new Merchant_Service_Customer();
		if($customerServiceObj->login($loginEmail,$password)){
            $customer = $customerServiceObj->getLoginInfo()->customer;
            //判断如果是停用，返回提示信息
            if($customer['customer_status']=='2'){
                $this->view->message = "Account has been disabled.";
                echo Cff::renderTpl("default/index/".$lang."-login.tpl");
                exit;
            }
			//登录成功
			$this->_redirect("/merchant/my-account/");
			exit;
		}
		$this->view->message = "Login failure";
        echo Cff::renderTpl("default/index/".$lang."-login.tpl");
		exit;
	}
	
	public function verifycodeAction() {
		$verifyCode = new Default_Service_Verifycode();
		$verifyCode->set_sess_name('UserVerifyCode');
		echo $verifyCode->render();
	}
	
	public function getpasswordAction() {
		$email = trim($this->_request->email);
		$verifyCode = trim($this->_request->verifyCode);
		$row =array('email'=>$email,'verifyCode'=>$verifyCode);
		$error = Merchant_Service_Customer::validateForGetPwd($row);
		if(count($error)){
			$data=array('ask'=>0,'error'=>$error);
			die(Zend_Json::encode($data));
		}else {

		}
		
	}

	public function logoutAction(){
		$customerServiceObj = new Merchant_Service_Customer();
		$customerServiceObj->logout();
		$this->_redirect("/index");
	}

	public function validateCustomer00Action(){
    	
		$task = $this->_request->task;
		$modify_email = $this->_request->getParam('modify_email','');
	
	    $customerServiceObj = new Merchant_Service_Customer();
	    $customer = $customerServiceObj->getLoginInfo()->customer;
        if( !$modify_email) $modify_email=$customer['customer_email'];
        else{
			if($customerServiceObj->existsEmail($modify_email)) $modify_email=$customer['customer_email']; 
		}
        $message = $customerServiceObj->validateCustomer($task,$modify_email);
		$this->view->message = $message;
		$this->view->modify_email = $modify_email;
      
        echo Cff::renderTpl("merchant/myaccount/step0.tpl","user-layout");
        exit;
	}
    
	public function informationAction(){
		
		$customerServiceObj = new Merchant_Service_Customer();
		$customer = $customerServiceObj->getLoginInfo()->customer;
		/*if($customer['customer_status']==0){
			$this->_redirect("/merchant/index/step0");
			exit;
		}
		*/
		//查找所有国家
		$countrys = Default_Service_Country::getAll();
		$this->view->countrys = $countrys;
		$customerServiceObj = new Merchant_Service_Customer();
		$customer = $customerServiceObj->getLoginInfo()->customer;
		$this->view->customer = $customer;
		echo Cff::renderTpl("merchant/myaccount/step1.tpl","user-layout");
	}

    public function step00000Action(){
        $customerServiceObj = new Merchant_Service_Customer();
        $customer = $customerServiceObj->getLoginInfo()->customer;
        $this->view->customer = $customer;
        $this->view->modify_email = $customer['customer_email'];
        echo Cff::renderTpl("merchant/myaccount/step0.tpl","user-layout");
        exit;
    }

	public function registerStepAction(){
		$firstName = trim($this->_request->getParam("firstName",""));
		$lastName = trim($this->_request->getParam("lastName",""));
		$phone = trim($this->_request->getParam("phone",""));
		$company = trim($this->_request->getParam("company",""));
		$country = trim($this->_request->getParam("country",""));
		$city = trim($this->_request->getParam("city",""));
		$state = trim($this->_request->getParam("state",""));
		$postalCode = trim($this->_request->getParam("postalCode",""));
		$address1 = trim($this->_request->getParam("address1",""));
		$address2 = trim($this->_request->getParam("address2",""));
		
		$customerRow = array(
			"customer_firstname" => $firstName,
			"customer_lastname" => $lastName,
			"customer_telephone" => $phone,
			"customer_company_name" => $company,
		);
		$addressRow = array(
			"cab_type" => 1,
			"cab_firstname" => $firstName,
			"cab_lastname" => $lastName,
			"cab_company" => $company,
			"cab_city" => $city,
			"cab_country_id" => $country,
			"cab_state" => $state,
			"cab_postcode" => $postalCode,
			"cab_street_address1" => $address1,
			"cab_street_address2" => $address2,
		);
		
		$serviceObj = new Merchant_Service_Customer();
		$addressObj = new Merchant_Service_CustomerAddressBook();
		$customerError = $serviceObj->validateCustomerInfo($customerRow);
		$addressError = $addressObj->validateAddressInfo($addressRow);
		$error = array_merge($customerError,$addressError);
		if(!empty($error)){
			$return = array(
				"ask" => false,
				"error" => $error,
			);
			die(json_encode($return));
		}
		$return = array();
		if($serviceObj->update($customerRow) && $addressObj->update($addressRow,1)){
			$serviceObj->update(array("reg_step" => 3));
			$customerServiceObj = new Merchant_Service_Customer();
			$customer = &$customerServiceObj->getLoginInfo()->customer;
			$customer['reg_step'] = 3;
			$return["ask"] = true;
		}else{
			$return["ask"] = false;
		}
		die(json_encode($return));
	}

	public function contactUs00000Action(){
		if(!$this->_request->isPost()){
			$stop = $this->_request->getParam('stop','');
			$this->view->stop = $stop;
			echo Cff::renderTpl("merchant/myaccount/contact-us.tpl","user-layout");
			exit;
		}
		$customerServiceObj = new Merchant_Service_Customer();
		$customer = $customerServiceObj->getLoginInfo()->customer;
		$email = $customer['customer_email'];
		$message = trim($this->_request->message);
		$row = array(
			"email" => $email,
			"message" => $message
		);
		$serviceObj = Default_Service_Index::getInstance();
		$serviceObj->addMessage($row,1);
		$this->view->message = "Add success";
		echo Cff::renderTpl("merchant/myaccount/contact-us.tpl","user-layout");
	}
	
}